;(function($){
	$.fn.sel_marcard=function(opts){
		var el = this;
		var opt = {
			oper : "s",//o:进行数据保存, s:返回选择积分卡数据;
			gno : 0,
			url:"/frontoffice/front/hoguest/procedure2",//表格url
			filterSearch : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
//		this.initSetValue();
		var t = this;
		setTimeout(function(){
			var arg = "create11"
			t.initGrid(arg);
		},100)
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//查询按钮
			this.el.find("#btnFilterSearch_MemberCard").click(function(){
				t.setGridValue();
				el.find("#profileInfo").find("span").each(function(k,v){
					$(v).text("");
				})
				el.find("#signaturebase64").attr("src","");
			});
			
			//保存
			this.el.find("#selectionModalSave_MemberCard").click(function(){
				var grid_selector = el.find("#gridTableMemberCard");
				var rowid = grid_selector.jqGrid("getGridParam","selrow");
				var rowData = grid_selector.jqGrid("getRowData",rowid);
				if(param.oper=="s"){
					param.dataBack(rowData);
					el.modal("hide");
					el.empty();
				}else if(param.oper=="o"){
					var para = {"args":{"gno":param.gno,"Field":"m_cardno","content":rowData.mno},"ho_guest":{}};
					var str = saveMcard(para);
					if(str==1){
						param.dataBack();
						el.modal("hide");
						el.empty();
					}
				}
			});
			
			//回车事件
			this.el.find("#frm").keydown(function(e){
				if(e.keyCode == 13){
					el.find("#btnFilterSearch_MemberCard").trigger("click");
				}
			})
			
			//关闭
			this.el.find("#close-dialog,#btnCancel").click(function(){
				param.closeBack();
				el.modal("hide");
				el.empty();
			});
			
			el.find("#signaturebase64").click(function(){

				var src = el.find("#signaturebase64").attr("src");
				var top = el.find("#signaturebase64").position().top;
				var left = el.find("#signaturebase64").position().left;
				el.find("#imgContainer").find("img").eq(0).attr("src",src);
				el.find("#imgContainer").css("top",200).css("left",100);
				el.find("#imgContainer").show();
			});
			
			//点击放大隐藏
			el.find("#photoHide").bind("click",function(){
				el.find("#imgContainer").hide();
			})
			
			//读卡
			el.find("#btnReadCard").click(function(){
				if(!$.isFunction($().oper_memberCard)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/jquery.modal.oper_memberCard.js");
		            $.ajaxSetup({ async: true });
				}
				
				var keyarg = [{Action:"Read",Cardno:""}];
				var log_key = "[]";
				
				$("").oper_memberCard({
					keyarg : JSON.stringify(keyarg),
					log_key : log_key,
					dataBack : function(data){
						var json_str = JSON.stringify(data).toLowerCase();
						var json = JSON.parse(json_str)
						el.find("#mname").val(json.cardno);
						el.find("#mname").trigger("change");
					}
				});
			})
			
			//清空
			el.find("#modalBtnClear").click(function(){
				el.find("#filterSearch").val("");
			})
		},
		initHtml : function(){
			
			this.el.addClass("modal").attr("tabindex","-1").attr("data-backdrop","static");
			var html=	'<div class="modal-dialog" style="min-width: 1000px;">'+
							'<div class="modal-content">'+
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+$.i18n.propJsp("请选择")+'&nbsp;'+'<i class="ace-icon fa fa-angle-double-right"></i>&nbsp;'+$.i18n.propJsp("会员卡")+'</span>'+
									'</div>'+
								'</div>'+
					  			'<div class="modal-body">'+
									'<div class="row">'+
										'<div id="imgContainer" class="widget-box" style="display:none;z-index:10;width:602px;position:absolute;">'+
											'<div class="widget-header">'+
												'<div class="widget-toolbar">'+
													'<button id="photoHide" type="button" class="close" value="HTML">'+
														'<span class="red bigger-200">&times;</span>'+
													'</button>'+
												'</div>'+
											'</div>'+
											'<div class="widget-body">'+
												'<img src="" style="width:600px;"></img>'+
											'</div>'+
										'</div>'+
										'<div class="col-xs-8">'+
											'<div class="well well-sm">'+
												'<span class="clearfix">'+
									  				'<span id="searchFilter" class="pull-left">'+
								  						'<input class="input-xlarge" id="filterSearch" value="'+this.param.filterSearch+'" type="text" placeholder="'+$.i18n.propJsp("卡号")+'/'+$.i18n.propJsp("名称")+'/'+$.i18n.propJsp("电话")+'"/>&nbsp;&nbsp;'+
								  						'<span class="btn-group" style="margin-top:-5px;">'+
															'<button id="modalBtnClear" class="btn btn-sm btn-info">'+
																'<i class="ace-icon fa fa-trash"></i>'+
																$.i18n.propJsp("清空")+
															'</button>'+
														'</span>'+
								  						'<span class="btn-group" style="margin-top:-5px;">'+
															'<button id="btnFilterSearch_MemberCard" class="btn btn-sm btn-info">'+
																'<i class="ace-icon fa fa-search"></i>'+
																$.i18n.propJsp("查询")+
															'</button>'+
														'</span>'+
													'</span>'+
												'</span>'+
											'</div>'+
											'<div class="widget-box">'+
								  				'<div id="gridWidth" class="widget-body">'+
													'<table id="gridTableMemberCard"></table>'+
													'<div id="grid-pager-checkbox_gridTableMemberCard"></div>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="col-xs-4">'+
											'<div class="widget-box">'+
												'<div class="widget-body">'+
													'<div class="widget-main" style="height:440px;">'+
														'<div id="profileInfo" class="sidebar-info">'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">mno</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="mno"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("组别")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="dcode_c"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("类型")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="type_c"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("电话")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="phone"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("卡号")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="mname"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("名称")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="text"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("余额")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="balance"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("档案编号")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="pno"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("客房费率连接")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="room_ratelink"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">POS'+$.i18n.propJsp("费率连接")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<span id="pos_ratelink"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">AR'+$.i18n.propJsp("余额")+'</div>'+
																'<div class="sidebar-info-value">'+
	//																'<img id="ar_balance" src="" style="width:180px;"/>'+
																	'<span id="ar_balance"></span>'+
																'</div>'+
															'</div>'+
															'<div class="sidebar-info-row">'+
																'<div class="sidebar-info-name">'+$.i18n.propJsp("签字留样")+'</div>'+
																'<div class="sidebar-info-value">'+
																	'<img id="signaturebase64" src="" style="width:180px;"/>'+
																'</div>'+
															'</div>'+
															
														'</div>'+
													'</div>'+
													'<div class="widget-toolbox padding-8 clearfix" style="height:53px;">'+
														'<div class="pull-right" style="margin-top:4px;">'+
															'<div class="btn-group">'+
																'<button id="btnReadCard" class="btn btn-info btn-sm">'+
																	'<i class="ace-icon fa fa-leanpub"></i>'+$.i18n.propJsp("读卡")+
																'</button>'+
															'</div>'+
															'<div class="btn-group">'+
																'<button class="btn btn-info btn-sm" id="selectionModalSave_MemberCard">'+
																	'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
																'</button>'+
															'</div>'+
															'<div class="btn-group">'+
																'<button id="btnCancel" class="btn btn-default btn-sm">'+
																	'<i class="ace-icon fa fa-times"></i>'+$.i18n.propJsp("取消")+
																'</button>'+
															'</div>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
					
			this.el.html(html);
		},
		initSetValue : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var dcode = sysgetdict("积分卡组别");
			el.find("#dcode").html('<option value="">'+$.i18n.propJsp("全部")+'</option>')
			$(dcode).each(function(key,value){
				el.find("#dcode").append('<option value="'+value.code+'">'+value.code+'--'+value.descript1+'</option>')
			})
			
			el.find("#chk_includegroup").trigger("click")
			el.find("#chk_onlyusable").trigger("click")
		},
		initGrid : function(arg){
			
			var grid_selector = this.el.find("#gridTableMemberCard");
			var pager_selector = this.el.find("#grid-pager-checkbox_gridTableMemberCard");
			grid_selector.jqGrid("GridUnload");
			var el = this.el;
//				var initc = this.getDs();
			var initc = "";
			var datatype = "json";
			if(arg=="create"){
				initc = {};
				datatype = "local";
			}else{
				initc = this.getDs();
				datatype = "json";
			}
			var param = this.param;
			//构造表格
			grid_selector.jqGrid({
				url : $.HSAPI_AJAXURL+ ""+param.url,
				datatype : datatype,
				width :643,
				height : 338,
				postData : $.HSAPI_ASKJSONINFO( JSON.stringify(initc),'ListRetrieveHoMArcustAndBoot',"{}","listWithPage"),
				colNames : ["集团/状态","集团","状态","","组别","积分卡卡号",
				            "积分卡名称","可用积分","积分","有效期","电话","次数","No","费率链接","签字留样","ar_balance","type_c"
				            ],
				colModel : [{
					name : "statuslabel",//状态
					index : "statuslabel",
					width: 80,
					align : "center",
					formatter:function(cellvalue,options,rowObject){
						var str = ''
						if(rowObject.isboot!=""){
							str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;">G</span>';
						}else{
							str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;"></span>';
						}
						if(rowObject.status!=1){
							str += "<span class='label label-danger'>"+$.i18n.propJsp("禁用")+"</span>"
						}else{
							str += "<span class='label label-danger'></span>"
						}
						return str;
					},
				},{
					name : "isboot",
					index : "isboot",
					width:60,
					align : "center",
					formatter : function(cellvalue,options,rowObject){
						var str = '';
						if(cellvalue!=""){
							str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;">G</span>';
						}
						return str;
					},
					hidden:true,
				},{
					name : "status",//状态
					index : "status",
					width: 60,
					hidden : true,
				},{
					name : "G",//"组别",
					index : "G",
					width: 40,
					align : "center",
					formatter : function(cellvalue,options,rowObject){
						var str = '';
						if(rowObject.mno<0){
							str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;">G</span>';
						}
						return str;
					},
					hidden:true,
				},{
					name : "dcode_c",//"组别",
					index : "dcode_c",
					width: 80,
				},{
					name : "mname",//"积分卡卡号",
					index : "mname",
					width: 140,
				},{
					name : "text",//"积分卡名称",
					index : "text",
					width : 140,
				},{
					name : "postlimit",//"可用积分",
					index : "postlimit",
					width : 100 ,
					align : "right",
				},{
					name : "balance_c",//"积分",
					index : "balance_c",
					width : 60,
					align : "right",
					hidden : true,
					formatter:function(cellvalue,options,rowObject){
						var str = ""
						if(rowObject.mno){
							
							str = (rowObject.c_balance||0)
						}else{
							
							str = (rowObject.balance||0)
						}
						return str
					},
				},{
					name : "ardate",//"有效期",
					index : "ardate",
					width : 100,
					hidden : true,
				},{
					name : "phone",//"电话",
					index : "phone",
					width : 120 ,
				},{
					name : "time_s",//"电话",
					index : "time_s",
					width : 120 ,
				},{
					name : "mno",//"No",
					index : "mno",
					width : 100,
					align : "right",
//					hidden : true,
				},{
					name : "room_ratelink",//"费率连接",room_ratelink
					index : "room_ratelink",
					width : 100,
					align : "right",
					hidden : true,
				},{
					name : "signaturebase64",//"签字图",
					index : "signaturebase64",
					width : 100,
					align : "right",
					hidden : true,
				},{
					name : "ar_balance",//"签字图",
					index : "ar_balance",
					width : 100,
					align : "right",
					hidden : true,
				},{
					name : "type_c",//"签字图",
					index : "type_c",
					width : 100,
					align : "right",
					hidden : true,
				}],    	
				sortname : "mname",
				sortorder : "asc",
				viewrecords : true,
//				rowNum : 20,
//				rowList:[10,20,30],
				emptyrecords: "没有搜索到任何数据",
				recordtext: "共计:{2}条",
				pager : pager_selector,
				altRows : true,  
				scrollrows:true,
				shrinkToFit : false,
				scroll: true,
			    onSelectRow:function(rowid){
			    	rowdata = grid_selector.jqGrid("getRowData",rowid);
		        	el.find("#profileInfo span").each(function(){
			    		$(this).text(rowdata[this.id]);
			    		$(this).attr("title",rowdata[this.id]);
			    	});
					el.find("#signaturebase64").attr("src",rowdata.signaturebase64);
		        },
	        	ondblClickRow: function(rowid) { 
	        		el.find("#selectionModalSave_MemberCard").trigger("click");
	        	},
	        	loadComplete : function(data) {
	        		var table = this;
					setTimeout(function(){
						updatePagerIcons(table);
					}, 0);
					if(param.loadComplete){
						param.loadComplete(grid_selector);
					}
				}, 
			});
			console.log(JSON.stringify(grid_selector.jqGrid))
		},
		setGridValue : function(){
			var grid_selector = this.el.find("#gridTableMemberCard");
			grid_selector.jqGrid("GridUnload");
			var arg = "fresh"
			this.initGrid(arg);
		},
		getDs : function(){
			var el = this.el;
			var param = this.param;
			var arg = {}
			arg.chk_includegroup = 1
			arg.chk_onlyusable = 1
			var all_search = el.find("#filterSearch").val();
			var mname = el.find("#filterSearch").val(); 
			var text = el.find("#filterSearch").val(); 
			var phone = el.find("#filterSearch").val(); 
			var json = {arg:{all_search:all_search},ho_marcust:{mname:mname,text:text,phone:phone},arg:arg}
//			var json = {arg:arg};
			return json
		},
		getGridCon : function(){
			var el = this.el;
			var str = el.find("#filterSearch_MemberCard").val();
			var arg = {"arg":{"all_search":str}};
			var str = "";
			jQuery.ajax({  
				type : 'POST',
				async:false, 
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO( JSON.stringify({}),'HoWSASCho_marcust_search',JSON.stringify(arg)),
				dataType : 'json',  
				success : function(data){
		            if(data.RETURN == 1){
		            	str = data.DATA;
		            }else{
						jf_alert(data.MSG);
					}
				},  
				error : function(data){  
					systoast($.i18n.propJsp("积分卡")+'HoWSASCho_marcust_search'+$.i18n.propJsp("错误提示")+':',$.i18n.propJsp("系统后台提交错")+'!' + JSON.stringify(data),'error');
				}  
			});
			return str;
		},
		//客人档案修改
		saveEditProfile:function(saveParam){
			
			var el = this.el;
			var param = this.param; 
			
			var t = this;
			var param = this.param;
			var str = "";
			//取数据并显示
			$.ajax({  
				type : 'POST',
				async:false, 
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(saveParam),'HoReservationProfileSaveEdit','{}'),
				dataType : 'json',  
				success : function(data){
		            if(data.RETURN == 1||data.RETURN == 0){
		            	if($.isFunction(param.frePgFun)){
							param.frePgFun();
		    		 	}
		            }else{
		            	str = data.MSG;
					}
				},  
				error : function(data){  
					systoast($.i18n.propJsp("修改陪同")+'HoReservationProfileSave'+$.i18n.propJsp("错误提示")+':',$.i18n.propJsp("系统后台提交错")+'!' + JSON.stringify(data),'error')
				}  
			});
			return str;
		},	
	}
	
	function saveMcard(para){
		var str = 0
		$("").dataChangeAsk($.HSAPI_AJAXURL,para,function(returnValue){//判断是否有级联字段一起修改
//    		alert("判断是否有级联字段一起修改:"+JSON.stringify(returnValue))
			if(returnValue.RETURN==0){//没有级联字段一起修改
				jf_alert(returnValue.MSG);
			}else if(returnValue.RETURN==1){//有级联字段一起修改
				var p = {};
				p.ho_guest = returnValue.ho_guest;
				$("").ho_reservation_save(p,$.HSAPI_AJAXURL,function(data){
					//保存失败
					jf_alert(data.MSG);
				},function(data){
					str = 1;
				});
			}else if(returnValue.RETURN==2){//有级联字段一起修改
				jf_alert(returnValue.MSG);
			}else{
				jf_alert(returnValue.MSG);
			}
		});
		return str;
	}
	
	/*
	 * 私有函数
	 * 翻页栏
	 * 
	 */
	function updatePagerIcons(table) {
		var replacement = {
				"ui-icon-seek-first" : "ace-icon fa fa-angle-double-left bigger-140",
				"ui-icon-seek-prev" : "ace-icon fa fa-angle-left bigger-140",
				"ui-icon-seek-next" : "ace-icon fa fa-angle-right bigger-140",
				"ui-icon-seek-end" : "ace-icon fa fa-angle-double-right bigger-140"
		};
		$(".ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon").each(function() {
			var icon = $(this);
			var $class = $.trim(icon.attr("class").replace("ui-icon", ""));

			if ($class in replacement)
				icon.attr("class", "ui-icon " + replacement[$class]);
		})
	};
})(jQuery);
